<?php

/**
 * This is the model class for table "auth".
 *
 * The followings are the available columns in table 'auth':
 * @property string $id
 * @property string $name
 * @property string $type
 * @property string $class
 * @property string $method
 * @property string $remark
 */
class Auth extends CActiveRecord
{
    public $searchKey = '';
    const TYPE_APP = 'app_api';
    const TYPE_API = 'default_api';

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'auth';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, class, method,type', 'required'),
            array('name', 'length', 'max' => 128),
            array('class, method,type', 'length', 'max' => 32),
            array('remark', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, name, type,class, method, remark, searchKey', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array();
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'name' => '名称',
            'type' => '类型',
            'class' => '类名',
            'method' => '方法',
            'remark' => '备注',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('class', $this->class, true);
        $criteria->compare('method', $this->method, true);
        $criteria->compare('remark', $this->remark, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function searchByKey($type = self::TYPE_API)
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria = new CDbCriteria;
        if ($this->searchKey !== '') {
            $criteria->addSearchCondition('name', $this->searchKey, true, 'OR');
            $criteria->addSearchCondition('class', $this->searchKey, true, 'OR');
            $criteria->addSearchCondition('method', $this->searchKey, true, 'OR');
            $criteria->addSearchCondition('remark', $this->searchKey, true, 'OR');
        }
        if ($this->type) {
            $criteria->compare('type', $this->type, true);
        }
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Auth the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public static function getAuthTree($inputName = '', $inputId = '', $on = array(), $htmlOptions = array(), $type = self::TYPE_API)
    {
        $tree = array();
        $auths = self::model()->findAllByAttributes(['type' => $type]);
        foreach ($auths as $auth) {
            if (!isset($tree[$auth->class])) {
                $tree[$auth->class] = array();
            }
            $tree[$auth->class][$auth->id] = $auth->name;
        }
        $element = '';
        $element .= '<div id="role_auth_tree">';
        foreach ($tree as $class => $auths) {
            $element .= '<div class="tree-branch" style="padding-left:20px;">';
            $element .= '<div class="checkbox">';
            $element .= '<label>';
            $element .= CHtml::checkBox($inputName, false, array('id' => $inputId . '_par', 'class' => 'par_perm', 'disabled' => true));
            $element .= '<a class="tree-branch-master" href="javascript:void(0)"><i class="fa fa-gears"></i>' . $class . '</a>';
            $element .= '</label>';
            $element .= '</div>';
            $element .= '<div class="tree-branch-subs" style="padding-left:20px;">';
            foreach ($auths as $id => $auth) {
                $element .= '<div class="checkbox">';
                $element .= '<label>';
                $element .= CHtml::checkBox($inputName, in_array($id, $on), array('id' => $inputId . '_sub', 'value' => $id, 'class' => 'sub_perm', 'disabled' => (isset($htmlOptions['disabled']))));
                $element .= '<a href="javascript:void(0)"><i class="fa fa-gear"></i>' . $auth . '</a>';
                $element .= '</label>';
                $element .= '</div>';
            }
            $element .= '</div>';
            $element .= '</div>';
        }
        $element .= '</div>';
        $element .= '<script type="text/javascript">';
        $element .= '$(function($) {';
        $element .= '	$("#role_auth_tree a.tree-branch-master").click(function(e){';
        $element .= '		$(this).parents(".tree-branch").children(".tree-branch-subs").toggle("fast");';
        $element .= '	});';
        $element .= '});';
        $element .= '</script>';
        return $element;
    }

    public static function getOptions($attribute = '')
    {
        $options = array();
        switch ($attribute) {
            case 'type':
                $options[self::TYPE_API] = '传统api类型';
                $options[self::TYPE_APP] = 'app类型';
                break;
            default:
                break;
        }
        return $options;
    }
}
